package com.woniu.service.impl;

import com.woniu.controller.form.DeleteStaff;
import com.woniu.dao.BmsPermissionMapper;
import com.woniu.model.BmsDept;
import com.woniu.model.BmsPermission;
import com.woniu.model.BmsRole;
import com.woniu.model.BmsUser;
import com.woniu.dao.BmsUserMapper;
import com.woniu.service.IBmsUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kevin Kwok
 * @since 2021-04-13
 */
@Service
@Transactional
public class BmsUserServiceImpl extends ServiceImpl<BmsUserMapper, BmsUser> implements IBmsUserService {

    @Resource
    private BmsUserMapper userMapper;
    @Resource
    private BmsPermissionMapper permissionMapper;

    @Override
    public BmsUser queryUserByTel(String tel) {
        BmsUser bmsUser = userMapper.queryUserByTel(tel);
        return bmsUser;
    }

    @Override
    public BmsUser queryUserByName(String name) {
        BmsUser bmsUser = userMapper.queryUserByName(name);
        return bmsUser;
    }

    //查询出当前用户的所有权限
    @Override
    public List<BmsPermission> queryUserPermission(BmsUser bmsUser) {
        //获取当前用户的所有权限
        List<BmsPermission> list = permissionMapper.queryAllMenuPermission(bmsUser);
        //定义一个集合暂时保存所有的一级菜单
        ArrayList<BmsPermission> rootList = new ArrayList<>();
        //取出所有的一级菜单
        for (BmsPermission bmsPermission : list) {
            //满足条件表示这是一个父级菜单
            if(bmsPermission.getPid()==0){
                //将父级菜单全部存入到临时的list里面去
                rootList.add(bmsPermission);
                //给当前的父级菜单设置其子菜单
                bmsPermission.setChildren(new ArrayList<BmsPermission>());
            }
        }
        //取出所有的二级菜单
        for (BmsPermission bmsPermission : list) {
            if(bmsPermission.getPid()!=0){
                for(BmsPermission root : rootList){
                    if(root.getId()==bmsPermission.getPid()){
                        root.getChildren().add(bmsPermission);
                        break;
                    }
                }
            }
        }
        //这是一个代码规范，list只是一个工具人，这里设置完了null的话，后面会被GC回收掉
        list = null;
        return rootList;
    }

    @Override
    public List<BmsPermission> queryAllUserPermission(BmsUser bmsUser) {
        List<BmsPermission> list = permissionMapper.queryAllMenuPermission(bmsUser);
        return list;
    }

    @Override
    public List<BmsUser> queryAllUser() {
        List<BmsUser> bmsUsers = userMapper.queryAllUser();
        return bmsUsers;
    }

    @Override
    public List<BmsDept> queryAllDept() {
        List<BmsDept> bmsDepts = userMapper.queryAllDept();
        return bmsDepts;
    }

    @Override
    public int addNewUser(BmsUser bmsUser) {
        //根据用户的手机检查该用户是否已经注册过，注册过就直接告诉信息
        BmsUser bmsUser1 = userMapper.queryUserByTel(bmsUser.getTel());
        if(bmsUser1!=null){
            return 0;
        }
        //该手机没有注册过，让其注册，返回成功
        int i = userMapper.addNewUser(bmsUser);
        return i;
    }

    @Override
    public int deleteStaff(DeleteStaff staff) {
        int i = userMapper.deleteStaff(staff);
        return i;
    }

    @Override
    public List<BmsRole> queryAllRole() {
        List<BmsRole> bmsRoles = userMapper.queryAllRole();
        return bmsRoles;
    }


}
